package com.mz.ozy.adapter;

import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.bean.MyOrderBean;
import com.mz.ozy.bean.MyOrderInfoBean;
import com.mz.ozy.utils.StringUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2018/11/22.
 */

public class MyOrderListAdapter extends BaseQuickAdapter<MyOrderBean.OrderBean.PageItemsBean, BaseViewHolder> {

    public MyOrderListAdapter(@Nullable List<MyOrderBean.OrderBean.PageItemsBean> data, String data1) {
        super(R.layout.view_item_my_order_obligation, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, MyOrderBean.OrderBean.PageItemsBean item) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        boolean isSingle = false;
        MyAdapter myAdapter;
        if (item.getProdcut_list() != null && !item.getProdcut_list().isEmpty()) {
            //只显示4张图片
            if (item.getProdcut_list().size() > 4) {
                ArrayList<MyOrderInfoBean.OrdermapBean.ProdcutListBean> goodsList = new ArrayList<>();
                for (int i = 0; i < 4; i++) {
                    goodsList.add(item.getProdcut_list().get(i));
                }
                myAdapter = new MyAdapter(goodsList);
            } else {
                myAdapter = new MyAdapter(item.getProdcut_list());
            }
            if (item.getProdcut_list().size() == 1) {
                isSingle = true;
            } else {
                isSingle = false;
            }
        } else {
            myAdapter = new MyAdapter(null);
            isSingle = false;
        }
        RecyclerView rvIcons = helper.getView(R.id.rv_icons);
        final RelativeLayout rlRvParent = helper.getView(R.id.rl_rv_parent);
        LinearLayout llSingleGoods = helper.getView(R.id.ll_single_goods);
        ImageView ivIcon = helper.getView(R.id.iv_icon);
        rvIcons.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return rlRvParent.onTouchEvent(event);
            }
        });
        if (isSingle) {
            rvIcons.setVisibility(View.GONE);
            llSingleGoods.setVisibility(View.VISIBLE);
            ivIcon.setVisibility(View.VISIBLE);
            final MyOrderInfoBean.OrdermapBean.ProdcutListBean prodcutListBean = item.getProdcut_list().get(0);
            String img_zt = "";
            if (!TextUtils.isEmpty(prodcutListBean.getImg_zt())) {
                final String[] splitImgZt = prodcutListBean.getImg_zt().split(",");
                if (splitImgZt.length > 0) {
                    img_zt = splitImgZt[0];
                }
            }
            GlideUtils.loadImg(mContext, ivIcon, img_zt, R.mipmap.default_foods_icon);
            helper.setText(R.id.tv_title, prodcutListBean.getProduct_name())
                    .setText(R.id.tv_info, "null".equals(prodcutListBean.getArrribute_name()) ? "" : prodcutListBean.getArrribute_name())
                    .setText(R.id.tv_count, "×" + item.getNum());
        } else {
            rvIcons.setVisibility(View.VISIBLE);
            ivIcon.setVisibility(View.GONE);
            llSingleGoods.setVisibility(View.GONE);
            myAdapter.setHasStableIds(true);
            rvIcons.setLayoutManager(linearLayoutManager);
            rvIcons.setAdapter(myAdapter);
        }

        if (helper.getAdapterPosition() == 0) {
            helper.setVisible(R.id.view_divider_top, true);
        } else {
            helper.setVisible(R.id.view_divider_top, false);
        }

        LinearLayout llBtn = helper.getView(R.id.ll_btn);
        TextView tvOrderId = helper.getView(R.id.tv_created);
        TextView tvOrderState = helper.getView(R.id.tv_order_state);
        TextView tvGoodsCount = helper.getView(R.id.tv_goods_count);
        TextView tvMoney = helper.getView(R.id.tv_money);
        TextView tvFreight = helper.getView(R.id.tv_freight);
        TextView tvTime = helper.getView(R.id.tv_time);

//        TextView tvChange = helper.getView(R.id.tv_change);
//        TextView tvCoupon = helper.getView(R.id.tv_coupon);
//        TextView tvXFjf = helper.getView(R.id.tv_xf_jf);

        llBtn.setVisibility(View.VISIBLE);
        helper.setVisible(R.id.view_order_bottom, true);
//        llChange.setVisibility(View.VISIBLE);

        tvOrderId.setText(item.getCreated());
        String yhPrice = "";
        if (!TextUtils.isEmpty(item.getYh_price())) {
            yhPrice = item.getYh_price();
        } else {
            yhPrice = "0.00";
        }
        tvGoodsCount.setText("总价¥" + getPrice(item) + ",优惠¥" + yhPrice + "  实付款");
        //现金总额
//        tvMoney.setText("¥" + item.getPrice());

        if (item.getType() == 1) {
            tvGoodsCount.setText("实付款");
            tvMoney.setText(item.getActual_price() + "欧花");

        } else {
            tvGoodsCount.setText("总价¥" + getPrice(item) + ",优惠¥" + yhPrice + "  实付款");
            tvMoney.setText("¥" + item.getActual_price());

        }

        tvFreight.setText("(运费¥" + StringUtils.formattingDecimal(item.getLogicost(), 2) + ")");

        TextView btnCancel = helper.getView(R.id.btn_cancel);
        TextView btnPay = helper.getView(R.id.btn_pay);
        TextView tvPayTime = helper.getView(R.id.tv_pay_time);
        TextView btnConfirm = helper.getView(R.id.btn_confirm);
        TextView btnPinl = helper.getView(R.id.btn_pinl);

        TextView btnLogistics = helper.getView(R.id.btn_logistics);
        TextView btnEvaluate = helper.getView(R.id.btn_evaluate);
        TextView btn_rerurn = helper.getView(R.id.btn_rerurn);

        helper.addOnClickListener(R.id.btn_cancel)
                .addOnClickListener(R.id.btn_pay)
                .addOnClickListener(R.id.btn_rerurn)
                .addOnClickListener(R.id.btn_confirm)
                .addOnClickListener(R.id.btn_logistics)
                .addOnClickListener(R.id.btn_pinl)
                .addOnClickListener(R.id.ll_item_view)
                .addOnClickListener(R.id.rl_rv_parent)
                .addOnClickListener(R.id.btn_evaluate);

        btnCancel.setVisibility(View.GONE);
        btnPay.setVisibility(View.GONE);
        tvPayTime.setVisibility(View.GONE);
        btnConfirm.setVisibility(View.GONE);
        btnLogistics.setVisibility(View.GONE);
        btnEvaluate.setVisibility(View.GONE);
        btnPinl.setVisibility(View.GONE);
        btn_rerurn.setVisibility(View.GONE);
        tvTime.setVisibility(View.GONE);
        //  订单状态；0-订单生成；1-已发货；2-已收货；3-退货中；4-已退货；5-已取消 6-未发货
        //  支付状态 0-生成，1-支付中，2-支付成功

        // 待付款：订单状态为0  支付状态为0
        // 待发货：订单状态为6 支付状态为2
        // 待收货：订单状态为1  支付状态为2

        tvOrderState.setText("未知");
        switch (item.getState()) {
            case 0://订单生成
                tvOrderState.setText("待支付");
                tvTime.setVisibility(View.VISIBLE);
                startCountDown(item, tvTime);
                btnCancel.setVisibility(View.VISIBLE);
                btnPay.setVisibility(View.VISIBLE);

                if (item.getTimeout() != null && item.getType() == MyOrderBean.OrderBean.PageItemsBean.TYPE_NO_TIMEOUT) {
                    tvPayTime.setVisibility(View.VISIBLE);
                }
                break;
            case 1: //已发货
                tvOrderState.setText("待发货");
                btn_rerurn.setVisibility(View.GONE);
                btnEvaluate.setVisibility(View.VISIBLE);
                break;
            case 2://已收货
                tvOrderState.setText("待收货");
                btnLogistics.setVisibility(View.VISIBLE);
                btnConfirm.setVisibility(View.VISIBLE);
                break;
            case 3://退货中
                tvOrderState.setText("待退款");
                btnEvaluate.setVisibility(View.VISIBLE);
                break;
            case 4://已退货
                tvOrderState.setText("已退款");
                btnEvaluate.setVisibility(View.VISIBLE);
                break;
            case 5://已取消
                if (item.getIsevaluation() == 0) {
                    btnPinl.setVisibility(View.VISIBLE);
                } else {
                    btnPinl.setVisibility(View.GONE);
                }
//                btnLogistics.setVisibility(View.VISIBLE);
                tvOrderState.setText("已完成");
                btnEvaluate.setVisibility(View.VISIBLE);
                break;
            case 6://未发货
                tvOrderState.setText("已取消");
                llBtn.setVisibility(View.GONE);
                helper.setGone(R.id.view_order_bottom, false);
                break;
            case 7://订单超时
                tvOrderState.setText("退款失败");
                llBtn.setVisibility(View.GONE);
                helper.setGone(R.id.view_order_bottom, false);
                break;
        }
    }

    private void startCountDown(MyOrderBean.OrderBean.PageItemsBean item, TextView view) {
        view.setText(mContext.getString(R.string.countdown_msg, Utils.long2DateStr(item.getSpaceTime())));
    }


    @NotNull
    public String getPrice(MyOrderBean.OrderBean.PageItemsBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getPrice()) && Double.parseDouble(item.getPrice()) != 0.00) {
            price += item.getPrice();
            price += "";
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += item.getMarket_price();
                price += "积分";
            } else {
                price += item.getMarket_price();
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "0.00";
        }
        return price;
    }

    @NotNull
    public int getPriceType(MyOrderBean.OrderBean.PageItemsBean item) {
        int type = -1;
        boolean isPrice = false;
        boolean isMarketPrice = false;
        if (!TextUtils.isEmpty(item.getPrice()) && Double.parseDouble(item.getPrice()) != 0.00) {
            isPrice = true;
            type = 0;
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            isMarketPrice = true;
            type = 1;
        }
        if (isPrice && isMarketPrice) {
            type = 2;
        }
        return type;
    }
}
